<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Sugus
 * @version 	: 1.1
 * COPYRIGHT    : 2011
 * ------------------------------------------------------
 *
 * Created on	:  Mar 08, 2011
 *
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RClassesImporter::cls('controllers.AbstractFrontController');

class ImageFrontController extends AbstractFrontController {
	/**
	 *
	 * Main model
	 * @var ImageModel
	 */
	var $_model;

	/**
	 *
	 * Constructor
	 */
	function __construct() {
		parent::__construct();
		SysApplication::importModelCls('ImageModel');
		$this->_model = new ImageModel();
	}

	/**
	 * (non-PHPdoc)
	 * @see src/core/mvc/AbstractController::display()
	 */
	function display() {
//		SysResponse::addJavascriptFile(SYS_TMPL_URL.'sugus/javascript/jquery-1.4.4.min.js');
//		SysResponse::addJavascriptFile(SYS_TMPL_URL.'sugus/javascript/jquery.prettyPhoto.js');
		//SysResponse::addStyleSheetFile(SYS_TMPL_URL.'sugus/css/prettyPhoto.css', 'screen');
		$script = 'jQuery(document).ready(function(){
			jQuery(".video-item a[rel^=\'prettyPhoto\']").prettyPhoto({animationSpeed:\'slow\',theme:\'facebook\',slideshow:2000, autoplay_slideshow: false});
		});';
		SysResponse::addJavascriptContent($script);
		$model = $this->_model;

		// Album section
		$xpage = SysRequest::getVar('apage', 1);
		$images = $model->getAlbumsPaging($xpage);
		$imageItems = $images->getRecordSet();
		$imagesList = '';
		$addi = '';
		foreach ($imageItems as $img) {
			$iImage = RubikHelper::checkAndAddImageURL($img->album_image, 149, 112);
			$iAlt = $img->album_name;
			$iText = $img->album_name;
			$linker = new SiteResource('album', 'details', $img->album_id);
			$iDetail = $linker->getLink();
			eval('$imagesList .= "'.SysApplication::loadTemplate('front/album-item').'";');
		}
		RCoreImporter::cls('html.Pagination');
		$pager = SysTemplate::getPaginationInstance($images->getTotalRecords(), 3, "?apage");
		$pager->setPagerText('<<','<','>','>>');
		$pagingHtml = $pager->renderPager($xpage);
		$titleImage = SYS_ROOT_URL.'templates/sugus/images/logo-hinhanh.png';
		$linker = new SiteResource('album', 'display');
		$imageShowLink = $linker->getLink();
		eval('$imagesHtml = "'.SysApplication::loadTemplate('front/albums').'";');

		// Video section
		$ypage = SysRequest::getVar('vpage', 1);
		$videos = $model->getVideoPaging($ypage, 2);
		$videoItems = $videos->getRecordSet();
		$videoList = '';
		$playerURL = SYS_ROOT_URL.'libraries/player/video/player.swf';
		foreach ($videoItems as $vid) {
			$videoLink = $vid->link;
			$videoName = $vid->name;
			$link = explode('?', $videoLink);
			$link2 = explode('/', $link[0]);
			$link1 = explode('=', $link[1]);
			$newLink = implode('/', array($link2[0], '', $link2[2], $link1[0], $link1[1]));
			$newLink .= '?fs=1&amp;hl=en_US&amp;showinfo=0&amp;showsearch=0&amp;rel=0';
			eval('$videoList .= "'.SysApplication::loadTemplate('front/video-item').'";');
		}
		$pagerVideo = SysTemplate::getPaginationInstance($videos->getTotalRecords(), 2, "?vpage");
		$pagerVideo->setPagerText('<<','<','>','>>');
		$pagingVideoHtml = $pagerVideo->renderPager($ypage);
		$linker = new SiteResource('video', 'display');
		$videoShowLink = $linker->getLink();
		$titleVideo = SYS_ROOT_URL.'templates/sugus/images/logo-video.png';
		eval('$videoHtml = "'.SysApplication::loadTemplate('front/videos').'";');

		eval('echo "'.SysApplication::loadTemplate('front/display').'";');
	}

	/**
	 *
	 * Show albums only
	 */
	function albums() {
		$model = $this->_model;
		$xpage = SysRequest::getVar('apage', 1);
		$limit = 9;
		$images = $model->getAlbumsPaging($xpage, $limit);
		$imageItems = $images->getRecordSet();
		$imagesList = '';
		$addi = ' style="margin-bottom:10px"';
		foreach ($imageItems as $img) {
			$iImage = RubikHelper::checkAndAddImageURL($img->album_image, 149, 112);
			$iAlt = $img->album_name;
			$iText = $img->album_name;
			$linker = new SiteResource('album', 'detail', $img->album_id);
			$iDetail = $linker->getLink();
			eval('$imagesList .= "'.SysApplication::loadTemplate('front/album-item').'";');
		}
		RCoreImporter::cls('html.Pagination');
		$pager = SysTemplate::getPaginationInstance($images->getTotalRecords(), $limit, "?apage");
		$pager->setPagerText('<<','<','>','>>');
		$pagingHtml = $pager->renderPager($xpage);
		$titleImage = SYS_ROOT_URL.'templates/sugus/images/logo-hinhanh.png';
		$linker = new SiteResource('image', 'albums');
		$imageShowLink = $linker->getLink();
		eval('echo "'.SysApplication::loadTemplate('front/album-only').'";');
	}

	/**
	 *
	 * Show videos only
	 */
	function videos() {
		SysResponse::addJavascriptFile(SYS_TMPL_URL.'sugus/javascript/jquery-1.4.4.min.js');
		SysResponse::addJavascriptFile(SYS_TMPL_URL.'sugus/javascript/jquery.prettyPhoto.js');
		SysResponse::addStyleSheetFile(SYS_TMPL_URL.'sugus/css/prettyPhoto.css', 'screen');
		$script = '$(document).ready(function(){
			$(".video-item a[rel^=\'prettyPhoto\']").prettyPhoto({animationSpeed:\'slow\',theme:\'facebook\',slideshow:2000, autoplay_slideshow: false});
		});';
		SysResponse::addJavascriptContent($script);
		$model = $this->_model;
		$ypage = SysRequest::getVar('vpage', 1);
		$limit = 6;
		$videos = $model->getVideoPaging($ypage, $limit);
		$videoItems = $videos->getRecordSet();
		$videoList = '';
		$addi = ' style="margin-bottom:10px"';
		$icon = SYS_ROOT_URL.'templates/sugus/images/samples/video.jpg';
		foreach ($videoItems as $vid) {
			$videoLink = $vid->link;
			$videoName = $vid->name;
			$link = explode('?', $videoLink);
			$link2 = explode('/', $link[0]);
			$link1 = explode('=', $link[1]);
			$newLink = implode('/', array($link2[0], '', $link2[2], $link1[0], $link1[1]));
			$newLink .= '?fs=1&amp;hl=en_US&amp;showinfo=0&amp;showsearch=0&amp;rel=0';
			eval('$videoList .= "'.SysApplication::loadTemplate('front/video-item').'";');
		}
		$pagerVideo = SysTemplate::getPaginationInstance($videos->getTotalRecords(), $limit, "?vpage");
		$pagerVideo->setPagerText('<<','<','>','>>');
		$pagingVideoHtml = $pagerVideo->renderPager($ypage);
		$titleVideo = SYS_ROOT_URL.'templates/sugus/images/logo-video.png';
		eval('echo "'.SysApplication::loadTemplate('front/video-only').'";');
	}
}